<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                date-format="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.buyTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('设备名称(编码)')">
              <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                  <span>{{ item.name }}</span>
                  <span v-if="item.name != '全部'">({{ item.id }})</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择设备类型')"
                  v-model="queryParam.deviceType"
                  dictCode="bis_device_info.type"
                />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="8" :lg="9" :md="10" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{ $t('今日') }} </a-menu-item>
                  <a-menu-item key="2"> {{ $t('近三天') }} </a-menu-item>
                  <a-menu-item key="3"> {{ $t('近七天') }} </a-menu-item>
                  <a-menu-item key="4"> {{ $t('近一月') }} </a-menu-item>
                  <a-menu-item key="5"> {{ $t('近三月') }} </a-menu-item>
                  <a-menu-item key="6"> {{ $t('近半年') }} </a-menu-item>
                  <a-menu-item key="7"> {{ $t('本年') }} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px">
                  {{ $t('快捷查询') }} <a-icon type="down"
                /></a-button>
              </a-dropdown>
              <a-button type="primary" @click="searchReset" style="margin-left: 8px">{{ $t('重置') }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" :loading="exloading" @click="handleExportXls($t('设备出币汇总'))" v-has="'BisOrderPhysicalCoinController:export'">{{
        $t('导出')
      }}</a-button>
      <a-button type="primary" v-print="'#printme'" v-has="'BisOrderPhysicalCoinController:print'">{{ $t('打印') }}</a-button>
    </div>

    <!-- table区域-begin -->
    <div id="printme">
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-dropdown>
            <a class="ant-dropdown-link">{{ $t('更多') }} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{ $t('详情') }}</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('笔数') }}：</span>
            <span>{{ totalInfo.count?totalInfo.count.toFixed(0):0 }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('出币值') }}：</span>
            <span>{{ totalInfo.amount?totalInfo.amount.toFixed(0):0 }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'DevicePhysicalCoinReport',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      moment,
      description: '设备出币汇总',
      // 表头
      columns: [
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceName',
        },
        {
          title: this.$t('设备类型'),
          align: 'center',
          dataIndex: 'deviceType_dictText',
        },
        {
          title: this.$t('笔数'),
          align: 'right',
          dataIndex: 'count',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('出币值'),
          align: 'right',
          dataIndex: 'amount',
          customRender: (text, r) => {
            return text.toFixed(0);
          }
        },
      ],
      url: {
        list: '/goods/bisOrderPhysicalCoin/total',
        exportXlsUrl: '/goods/bisOrderPhysicalCoin/exportXlsTotal',
        totalUrl: '/goods/bisOrderPhysicalCoin/total',
      },
      dictOptions: {},
      superFieldList: [],
      isfooter: true,
      deviceList: [],
      queryParam: {
        groupBy: 'deviceId',
        buyTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        buyTime_end: moment().format('YYYY-MM-DD 23:59:59'),
      },
      isCustom: true,
      isHideData: false,
      hideCond:1,
      isOptArea: true
    }
  },
  created() {
    this.getSuperFieldList()
  },
  mounted() {
    let that = this
    document.onkeydown = function (e) {
      let key = window.event.keyCode
      if (key == 75 && event.ctrlKey) {
        window.event.preventDefault()
        that.isHideData = false
        that.queryParam.dataType = 0
        that.searchQuery()
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      this.queryParam.groupBy = 'deviceId'
      this.queryParam.deviceId = ''
      this.customInit()
    },
    customInit() {
      this.handleSearch('')
      this.isHideData = true
      this.queryParam.dataType = 1
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {
        groupBy: 'deviceId',
        deviceId: '',
        buyTime_begin: moment().format('YYYY-MM-DD 00:00:00'),
        buyTime_end: moment().format('YYYY-MM-DD 23:59:59'),
      }
      this.selectedRowIndex = []
      this.customInit()
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    getTotalQueryParams(params) {
      var totalParams = Object.assign({}, params)
      totalParams.groupBy = ''
      totalParams.pageNo = 1
      totalParams.pageSize = 1
      return totalParams
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'orderId', text: this.$t('订单号'), dictCode: '' })
      fieldList.push({ type: 'datetime', value: 'buyTime', text: this.$t('购买时间') })
      fieldList.push({
        type: 'string',
        value: 'orderType',
        text: this.$t('订单类型'),
        dictCode: 'bis_order.order_type',
      })
      fieldList.push({ type: 'string', value: 'goodsName', text: this.$t('商品名称'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'amount', text: this.$t('数量'), dictCode: '' })
      fieldList.push({ type: 'int', value: 'state', text: this.$t('取货状态'), dictCode: 'bis_order_goods.state' })
      fieldList.push({ type: 'datetime', value: 'takeTime', text: this.$t('取货时间') })
      fieldList.push({ type: 'int', value: 'takeNum', text: this.$t('已取数量'), dictCode: '' })
      this.superFieldList = fieldList
    },
    handleDateClick(evt) {
      if (evt.key == '1') {
        this.queryParam.buyTime_begin = moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '2') {
        this.queryParam.buyTime_begin = moment().subtract(2, 'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '3') {
        this.queryParam.buyTime_begin = moment().subtract(6,'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '4') {
        this.queryParam.buyTime_begin = moment().subtract(1, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '5') {
        this.queryParam.buyTime_begin = moment().subtract(3, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '6') {
        this.queryParam.buyTime_begin = moment().subtract(6, 'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      if (evt.key == '7') {
        this.queryParam.buyTime_begin = moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.buyTime_end = moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>